package cn.csc.module.product.controller.admin.spu;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.spu.vo.*;
import cn.csc.module.product.convert.spu.SpuConvert;
import cn.csc.module.product.dal.dataobject.spu.SpuDO;
import cn.csc.module.product.service.spu.SpuService;
import cn.csc.module.product.validated.Verify1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - spu信息")
@RestController
@RequestMapping("/product/spu")
public class SpuController {

    @Resource
    private SpuService spuService;

    @PostMapping("/create")
    @ApiOperation("创建spu信息")
    @PreAuthorize("@ss.hasPermission('product:spu:create')")
    public CommonResult<BigInteger> createSpu(@Validated @RequestBody SpuCreateDataReqVO createDataReqVO) {
        return success(spuService.createSpu(createDataReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新spu信息")
    @PreAuthorize("@ss.hasPermission('product:spu:update')")
    public CommonResult<Boolean> updateSpu(@Validated(Verify1.class) @RequestBody SpuCreateDataReqVO createDataReqVO) {
        spuService.updateSpu(createDataReqVO);
        return success(true);
    }


    @PostMapping("/downline")
    @ApiOperation("商品上下架")
    @PreAuthorize("@ss.hasPermission('product:spu:online')")
    public CommonResult<Boolean> downline(@RequestBody SpuUpdateReqVO updateReqVO) {
        SpuUpdateReqVO reqVO = new SpuUpdateReqVO();
        reqVO.setId(updateReqVO.getId());
        reqVO.setVerifyStatus(updateReqVO.getVerifyStatus());
        spuService.updateShelf(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除spu信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu:delete')")
    public CommonResult<Boolean> deleteSpu(@RequestParam("id") BigInteger id) {
        spuService.deleteSpu(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品详情信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu:query')")
    public CommonResult<SpuDataRespVO> getSpu(@RequestParam("id") BigInteger id) {
        SpuDataRespVO spu = spuService.getSpuById(id);
        return success(spu);
    }

    @GetMapping("/list")
    @ApiOperation("获得spu信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:spu:query')")
    public CommonResult<List<SpuRespVO>> getSpuList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SpuDO> list = spuService.getSpuList(ids);
        return success(SpuConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得spu信息分页")
    @PreAuthorize("@ss.hasPermission('product:spu:query')")
    public CommonResult<PageResult<SpuRespVO>> getSpuPage(SpuPageReqVO pageVO) {
        PageResult<SpuDO> pageResult = spuService.selectPageByOnLineTimeDesc(pageVO);
        return success(SpuConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出spu信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuExcel(@Valid SpuExportReqVO exportReqVO,
                               HttpServletResponse response) throws IOException {
        List<SpuDO> list = spuService.getSpuList(exportReqVO);
        // 导出 Excel
        List<SpuExcelVO> datas = SpuConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "spu信息.xls", "数据", SpuExcelVO.class, datas);
    }

}
